/*
 * Copyright (C) 2007-2017 FBReader.ORG Limited <contact@fbreader.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

package org.geometerplus.fbreader.formats;

import android.app.Application;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.ParcelFileDescriptor;

import com.htfyun.utils.DebugLog;
import com.shockwave.pdfium.PdfDocument;
import com.shockwave.pdfium.PdfiumCore;

import org.geometerplus.zlibrary.core.application.ZLApplication;
import org.geometerplus.zlibrary.core.image.ZLImage;
import org.geometerplus.zlibrary.core.util.RationalNumber;
import org.geometerplus.zlibrary.ui.android.image.ZLBitmapImage;
import org.pdfparse.model.PDFDocInfo;
import org.pdfparse.model.PDFDocument;

import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.util.SystemInfo;

import org.geometerplus.fbreader.book.AbstractBook;
import org.geometerplus.fbreader.book.BookUtil;

import java.io.File;
import java.io.IOException;
import java.net.URI;

public class PDFPlugin extends ExternalFormatPlugin {
	public PDFPlugin(SystemInfo systemInfo) {
		super(systemInfo, "PDF");
	}

	@Override
	public String packageName() {
		return "org.geometerplus.zlibrary.ui.android";
	}

	@Override
	public ZLImage readCover(ZLFile file) {

		Bitmap render;
		try {
			
			PdfiumCore core = new PdfiumCore();
			ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file.getPhysicalFile().javaFile()
					, ParcelFileDescriptor.MODE_READ_ONLY);
			PdfDocument pdfDocument = core.newDocument(pfd);

			final int pageIndex = 0;

			core.openPage(pdfDocument, pageIndex);

			int height = core.getPageHeight(pdfDocument, pageIndex) / 10;
			int width = core.getPageWidth(pdfDocument, pageIndex) / 10;

			if (height == 0 || width == 0) {
				height = 250;
				width = 200;
			} else {

				while (height > 250 || width > 200) {
					height -= 10;
					width -= 10;
				}
				
			}
			
			render = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

			core.renderPageBitmap(pdfDocument, render, pageIndex,
					0, 0,
					width, height, false);

			core.closeDocument(pdfDocument);
			pdfDocument = null;
			core = null;

			return new ZLBitmapImage(render);

			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		
	}

	@Override
	public void readMetainfo(AbstractBook book) {
		DebugLog.e("read pdf metainfo");
		final ZLFile file = BookUtil.fileByBook(book);
		if (file != file.getPhysicalFile()) {
			// TODO: throw BookReadingException
			System.err.println("Only physical PDF files are supported");
			return;
		}
		try {
//			final PDFDocument doc = new PDFDocument(book.getPath());
//			// TODO: solution for rc4 encryption
//			if (!doc.isEncrypted()) {
//				final PDFDocInfo info = doc.getDocumentInfo();
//				book.setTitle(info.getTitle());
//				book.addAuthor(info.getAuthor());
//			}

			PdfiumCore core = new PdfiumCore();
			ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file.getPhysicalFile().javaFile()
					, ParcelFileDescriptor.MODE_READ_ONLY);
			PdfDocument pdfDocument = core.newDocument(pfd);

			PdfDocument.Meta meta = core.getDocumentMeta(pdfDocument);
			int pageCount = core.getPageCount(pdfDocument);

			book.setTitle(meta.getTitle());
			book.addAuthor(meta.getAuthor());
			book.setProgress(RationalNumber.create(1, pageCount));
			
			DebugLog.e("pdf title = " + meta.getTitle() + ", author = " + meta.getAuthor() + ", pageCount = " + pageCount);
			
			core.closeDocument(pdfDocument);
			pdfDocument = null;
			core = null;
			
			
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}



	@Override
	public void readUids(AbstractBook book) {
		if (book.uids().isEmpty()) {
			book.addUid(BookUtil.createUid(book, "SHA-256"));
		}
	}
}
